/*** 
 * @_______________#########_______________________ 
 * @______________############_____________________ 
 * @______________#############____________________ 
 * @_____________##__###########___________________ 
 * @____________###__######_#####__________________ 
 * @____________###_#######___####_________________ 
 * @___________###__##########_####________________ 
 * @__________####__###########_####_______________ 
 * @________#####___###########__#####_____________ 
 * @_______######___###_########___#####___________ 
 * @_______#####___###___########___######_________ 
 * @______######___###__###########___######_______ 
 * @_____######___####_##############__######______ 
 * @____#######__#####################_#######_____ 
 * @____#######__##############################____ 
 * @___#######__######_#################_#######___ 
 * @___#######__######_######_#########___######___ 
 * @___#######____##__######___######_____######___ 
 * @___#######________######____#####_____#####____ 
 * @____######________#####_____#####_____####_____ 
 * @_____#####________####______#####_____###______ 
 * @______#####______;###________###______#________ 
 * @________##_______####________####______________ 
 * @
 * @Author: ipk518 18163976442@163.com
 * @Date: 2024-06-19 10:31:14
 * @LastEditors: ipk518 121206530@qq.com
 * @LastEditTime: 2025-06-26 19:53:36
 * @FilePath: /etws-sxk2307/src/packetResolver/lxCmdV1/sysProcess.cpp
 * @Description: 
 * @************佛祖保佑************
 * @Copyright (c) 2024 by etws@quyujiang, All Rights Reserved. 
 */

#include "Server.h"
#include "lxCmdV1.h"

//系统启动
int work_callback_0(struct cmdDataType* cmd,uint8_t * cmdData)
{
    Server::Instance().m_sxk.m_controlReg.m_timingStatus = TIME_START;//产生正常时序
    Server::Instance().m_event.triggerEvent(WRITE_CONTROL_REG);
    return Server::Instance().responseWeb(cmd);
}

//系统关闭
int work_callback_1(struct cmdDataType* cmd,uint8_t * cmdData)
{
    Server::Instance().m_sxk.m_controlReg.m_timingStatus = TIME_STOP;//关闭时序
    Server::Instance().m_event.triggerEvent(WRITE_CONTROL_REG);
    return Server::Instance().responseWeb(cmd);
}

//工作/测试模式
int work_callback_2(struct cmdDataType* cmd,uint8_t * cmdData)
{
    //0：工作模式;1：硬件调测模式(默认);2：外标校波束校准模式;3：暗室测试模式;
    Server::Instance().m_sxk.hardwareSync();
    if (*cmdData != 0) {
        Server::Instance().m_sxk.m_sxkHardTest.test_enable = 1;
    } else {
        Server::Instance().m_sxk.m_sxkHardTest.test_enable = 0;
    }
    Server::Instance().m_event.triggerEvent(WRITE_HARDWARE_TEST);

    return Server::Instance().responseWeb(cmd);
}

//打开辐射-tr功放总开关
int work_callback_4(struct cmdDataType* cmd,uint8_t * cmdData)
{
    Server::Instance().m_sxk.hardwareSync();
    Server::Instance().m_sxk.m_sxkHardTest.radiate_switch = 1;
    Server::Instance().m_event.triggerEvent(WRITE_HARDWARE_TEST);
    return Server::Instance().responseWeb(cmd);
}
//关闭辐射-tr功放总开关 todo:2023/8/11
int work_callback_5(struct cmdDataType* cmd,uint8_t * cmdData)
{
    Server::Instance().m_sxk.hardwareSync();
    Server::Instance().m_sxk.m_sxkHardTest.radiate_switch = 0;
    Server::Instance().m_event.triggerEvent(WRITE_HARDWARE_TEST);
    return Server::Instance().responseWeb(cmd);
}

/*** 
 * @description: TR发射频通道配置TODO:2025/4/19
 * @param {cmdDataType*} cmd
 * @param {uint8_t *} cmdData
 * @return {*}
 */
int work_callback_8(struct cmdDataType* cmd,uint8_t * cmdData)
{
    uint8_t data[6] = {0,0,0,0,0,0};
    uint8_t m_tmp = 0;
    Server::Instance().m_sxk.hardwareSync();
    memcpy(data,cmdData,6);
    if(data[0] == 1) {
        m_tmp = m_tmp | 0x1;
    }
    if (data[1] == 1) {
        m_tmp = m_tmp | 0x2;
    }
    Server::Instance().m_sxk.m_sxkHardTest.tr_receive_switch = m_tmp;
    Server::Instance().m_sxk.m_sxkHardTest.tr0_send_switch = (uint16_t)(data[3] << 8 | data[2]);
    Server::Instance().m_sxk.m_sxkHardTest.tr1_send_switch = (uint16_t)(data[5] << 8 | data[4]);
    Server::Instance().m_event.triggerEvent(WRITE_HARDWARE_TEST);
    return Server::Instance().responseWeb(cmd);
}

//方位角消除-时序卡
int work_callback_9(struct cmdDataType* cmd,uint8_t * cmdData)
{
    Server::Instance().m_sxk.hardwareSync();
    if(cmd->dataLen >= 4){

        Server::Instance().m_sxk.m_sxkHardTest.az_start = *((uint16_t *)cmdData);
        Server::Instance().m_sxk.m_sxkHardTest.az_end = *((uint16_t *)(cmdData+2));
        Server::Instance().m_sxk.m_sxkHardTest.az_switch = 1;
        //开启写入波位控制寄存器
        Server::Instance().m_event.triggerEvent(WRITE_HARDWARE_TEST);
        return Server::Instance().responseWeb(cmd);
    }
    return 0;
}

int work_callback_12(struct cmdDataType* cmd,uint8_t * cmdData)
{
    Server::Instance().m_sxk.hardwareSync();
    // return Server::Instance().responseWeb(cmd);
    return 0;
}

int work_callback_13(struct cmdDataType* cmd,uint8_t * cmdData)
{
    Server::Instance().m_sxk.hardwareSync();
    // return Server::Instance().responseWeb(cmd);
    return 0;
}

/*** 
 * @description: 新增相位编码使能0x09-0x0e
 * @param {cmdDataType*} cmd
 * @param {uint8_t *} cmdData
 * @return {*}
 */
int work_callback_14(struct cmdDataType* cmd,uint8_t * cmdData)
{
    Server::Instance().m_sxk.hardwareSync();
    Server::Instance().m_sxk.m_sxkHardTest.en_encoding = *cmdData;
    Server::Instance().m_event.triggerEvent(WRITE_HARDWARE_TEST);
    return Server::Instance().responseWeb(cmd);
}

/*** 
 * @description: 超低副瓣使能0x09-0x12
 * @param {cmdDataType*} cmd
 * @param {uint8_t *} cmdData
 * @return {*}
 */
int work_callback_18(struct cmdDataType* cmd,uint8_t * cmdData)
{
    Server::Instance().m_sxk.hardwareSync();
    Server::Instance().m_sxk.m_sxkHardTest.ad_sidelobe = *cmdData;
    Server::Instance().m_event.triggerEvent(WRITE_HARDWARE_TEST);
    return Server::Instance().responseWeb(cmd);
}
